﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using spbtzb;


public partial class Back_BgArticleEdit : System.Web.UI.Page
{ 
    #region Field
    protected String StrConn = ConString.ConnString;

    private long view_ID
    {
        get
        {
            return ViewState["view_ID"] == null ? -1 : Convert.ToInt64(ViewState["view_ID"]);
        }
        set
        {
            ViewState["view_ID"] = value;

        }
    }

    private Boolean session_Logined
    {
        set
        {
            Session["Logined"] = value;
        }
        get
        {
            return Session["Logined"] == null ? false : Convert.ToBoolean(Session["Logined"]);
        }
    }
    #endregion 

    #region 

    public int MaxID //获得最大ID
    {
        get
        {
            string sqlCmd = "SELECT * FROM ContentType order by ID desc";
            System.Data.DataTable dtTypes = AccessHelper.ExecuteDataTable(StrConn, System.Data.CommandType.Text, sqlCmd);
            if (dtTypes.Rows.Count == 0)//dtTypes == null || 
            {
                // 没有相应类型的信息
                RegisterClientScriptBlock("tip", "<script>alert('没有相应的类型数据！');</script>");
                return 0;
            }
            return int.Parse(dtTypes.Rows[0][0].ToString());
        }
    }

    public string ContentClientID //获得ftb的客户端ID
    {
        get
        {
            return ftb_Content.ClientID;
        }
    }

    public string FieldClientID //获得Field的客户端ID
    {
        get
        {
            return chkvalue.ClientID;
        }
    }


    public string SpValueID
    {
        get
        {
            return SpValue.ClientID;
        }
    }
    #endregion



    #region
    public DataTable ParentType
    {
        get
        {
            //         string sqlCmd = "SELECT * FROM ContentType WHERE ParentType = 'NO' OR ParentType = 'NOBB' OR ParentType = 'STBB'";
            string sqlCmd = "SELECT distinct C.* FROM ContentType C ,ContentType CT WHERE (( C.ParentType = 'NO' OR C.ParentType = 'STBB') And (C.TypeCode= CT.ParentType)) OR C.ParentType='NOBB'";
            System.Data.DataTable dtTypes = AccessHelper.ExecuteDataTable(StrConn, System.Data.CommandType.Text, sqlCmd);

            if (dtTypes.Rows.Count == 0)//dtTypes == null || 
            {
                // 没有相应类型的信息
                RegisterClientScriptBlock("tip", "<script>alert('没有相应的类型数据！');</script>");
                return new DataTable();
            }
            return dtTypes;
        }

    }

    public DataTable SelectedCheckbox 
    {
        get 
        {
            if (Request["ID"] == null)
            {
                // 跳转到错误处理页面
                Response.Redirect("~/Common/ErrorPage.aspx?ErrorMessage=" + "跳转参数为空，请联系管理员!");
            }
            if (session_Logined == false)
            {
                Response.Redirect("Login.aspx?ErrorMessage=" + "您未登录，请在此登录");
            }
            //StrConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='E:\\spbtzbDB\\spbtzb.mdb';";
            string view_ID = Convert.ToInt64(Request["ID"]).ToString();

            DataTable Table1 = AccessHelper.ExecuteDataTable(StrConn, CommandType.Text, "select T.TypeCode from ContentType T,ContentAndType CT where T.ID=CT.TypeID  And CT.ContentID=" + view_ID);
            return Table1;
        }
    }

    public string NoticeChecked 
    {
        get 
        {
            DataTable table = SelectedCheckbox;
            for (int i = 0; i < table.Rows.Count; i++) 
            {
                string code = table.Rows[i]["TypeCode"].ToString();
                if (table.Rows[i]["TypeCode"].ToString() == "WZGG") 
                {
                    return "checked";
                }
                
                
            }
            return "";
        }
    }
    public DataTable ChildType
    {
        get
        {
            string sqlCmd = String.Format("SELECT * FROM ContentType ");

            System.Data.DataTable dtChildTypes = AccessHelper.ExecuteDataTable(StrConn, System.Data.CommandType.Text, sqlCmd);
            if (dtChildTypes.Rows.Count == 0)
            {
                RegisterStartupScript("没有相应数据", "<script>alert('没有相关的数据！');</script>");
                return new DataTable();
            }

            return dtChildTypes;
        }
    }
   #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Page.Title = "文章编辑页面";
        if (!Page.IsPostBack)
        {
            if (Request["ID"] == null)
            {
                // 跳转到错误处理页面
                Response.Redirect("~/Common/ErrorPage.aspx?ErrorMessage=" + "跳转参数为空，请联系管理员!");
            }
            if (session_Logined == false)
            {
                Response.Redirect("Login.aspx?ErrorMessage=" + "您未登录，请在此登录");
            }
            //StrConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='E:\\spbtzbDB\\spbtzb.mdb';";
            view_ID = Convert.ToInt64(Request["ID"]);    
            Ddl_Types_BindData();
        }

    }

    protected void Submit_Btn_Click(object sender, EventArgs e)
    {
       if (view_ID > 0)
       {
           long lID = view_ID;
           String strContent = spbtzb.AuthenCtr.Encode(this.ftb_Content.Text.TrimEnd());
           String strTypeCode = "";
           



           /*
           string sqlCmd = String.Format("Update Contents SET TypeCode = '{0}', Title = '{1}', Content = '{2}' "
                           + "WHERE ID = {3}", strTypeCode, this.tb_Title.Text.Trim(),
                           strContent, lID);
                          // this.tb_Content.Text, lID);
            */
           
           string sqlCmd = String.Format("Update Contents SET Title = '{0}', Content = '{1}' WHERE ID = {2}", this.tb_Title.Text.Trim(), strContent, lID);
           int iret=AccessHelper.ExecuteNonQuery(StrConn,CommandType.Text,sqlCmd);
           string sqlSmdDele="Delete * from ContentAndType where ContentID=" +lID.ToString();
           int iret2=AccessHelper.ExecuteNonQuery(StrConn,CommandType.Text,sqlSmdDele);
           List<string> listType;
           if (chkvalue.Text == "") 
           {
               listType=new List<string>();
           }else
           {
               listType=chkvalue.Text.Split(',').ToList();
           }
           if (SpValue.Text != "")
           {
               listType.Add(SpValue.Text);
           }
           foreach (var c in listType) 
           {
                string sqlGetTypeID =  String.Format("Select ID From ContentType Where TypeCode='{0}'", c);
                DataTable datatable1= AccessHelper.ExecuteDataTable(StrConn, CommandType.Text, sqlGetTypeID);
                int TypeID = int.Parse(datatable1.Rows[0]["ID"].ToString());
                string sqlCmd1 = String.Format("Insert into ContentAndType(ContentID,TypeID,ContentType) values('{0}','{1}','{2}')", lID, TypeID, c);
                
                AccessHelper.ExecuteNonQuery(StrConn, CommandType.Text, sqlCmd1);
           } 
            
           

           int iRet = AccessHelper.ExecuteNonQuery(StrConn, System.Data.CommandType.Text, sqlCmd);  
           if (iRet > 0)
           { 
               Response.Write("<script>alert('编辑成功！');</script>");
           }       
           else
           {
               // 跳转到错误处理页面
               Response.Redirect("~/Common/ErrorPage.aspx?ErrorMessage=" + "修改文章操作失败，请联系管理员!");
           }
       }

    }
    protected void Cancel_Btn_Click(object sender, EventArgs e)
    {
        Response.Redirect("BgArticleList.aspx");
        //RegisterClientScriptBlock("tip", "<script>window.close();</script>");
    }

    private void Ddl_Types_BindData()
    {                     
        // generate datatable                       
        string sqlCmd = "SELECT * FROM ContentType WHERE ParentType = 'NO' OR ParentType = 'NOBB' OR ParentType = 'STBB'";
        System.Data.DataTable dtTypes = AccessHelper.ExecuteDataTable(StrConn, System.Data.CommandType.Text, sqlCmd);
        if (dtTypes == null || dtTypes.Rows.Count == 0)
        {
            // 没有相应类型的信息
            RegisterClientScriptBlock("tip", "<script>alert('没有相应的类型数据！');</script>");
            return;
        }
        


        sqlCmd = String.Format("SELECT * FROM Contents WHERE ID = {0}", view_ID);
        System.Data.DataTable dtContent = AccessHelper.ExecuteDataTable(StrConn, System.Data.CommandType.Text, sqlCmd);
        if (dtContent.Rows.Count == 0)
        { 
            // 没有相应类型的信息
            //RegisterClientScriptBlock("tip", "<script>alert('没有相应的类型数据！');</script>");
            //return;
            Response.Redirect("~/Back/Login.aspx?ErrorMessage=" + "没有相应的类型数据，请重新操作或联系管理员");
        }
        DataRow dr = dtContent.Rows[0];
        //this.ddl_ChildTypes.SelectedValue = dr["TypeCode"].ToString(); // 绑定到子类别_错
        String strChildType = dr["TypeCode"].ToString(); 
        this.tb_Title.Text = dr["Title"].ToString();
        ftb_Content.Text = spbtzb.AuthenCtr.Decode(dr["Content"].ToString());


    }


}
